package com.tjmc.recruit.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjmc.recruit.admin.api.entity.PlatformUserInfoEntity;
import com.tjmc.recruit.admin.api.entity.SessionInfoEntity;
import com.tjmc.recruit.admin.api.feign.RemoteUserService;
import com.tjmc.recruit.admin.api.vo.ChatVO;
import com.tjmc.recruit.admin.api.vo.CurrentUserSessionVO;
import com.tjmc.recruit.admin.mapper.SessionInfoMapper;
import com.tjmc.recruit.admin.service.SessionInfoService;
import com.tjmc.recruit.common.core.constant.CommonConstants;
import com.tjmc.recruit.common.core.constant.SecurityConstants;
import com.tjmc.recruit.common.core.util.R;
import com.tjmc.recruit.common.security.util.SecurityUtils;
import com.tjmc.recruit.finance.api.dto.AccountChangeDTO;
import com.tjmc.recruit.finance.api.entity.PlatformUserAccountEntity;
import com.tjmc.recruit.finance.api.entity.UsageRecordsEntity;
import com.tjmc.recruit.finance.api.enums.ChangeMehtodEnum;
import com.tjmc.recruit.finance.api.enums.UsageServiceTypeEnum;
import com.tjmc.recruit.finance.api.enums.UsageTypeEnum;
import com.tjmc.recruit.finance.api.feign.RemoteAccountService;
import com.tjmc.recruit.finance.api.feign.RemoteUsageRecordsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

/**
 * 会话信息表
 *
 * @author recruit
 * @date 2024-12-03 16:02:27
 */
@Service
@RequiredArgsConstructor
public class SessionInfoServiceImpl extends ServiceImpl<SessionInfoMapper, SessionInfoEntity> implements SessionInfoService {
    private final RemoteAccountService remoteAccountService;
    private final RemoteUsageRecordsService remoteUsageRecordsService;
    private final RemoteUserService remoteUserService;
    @Override
    public List<CurrentUserSessionVO> queryCurrentUserSessionList() {
        // 1.获取当前登录用户ID
        String name = SecurityUtils.getUser().getName();
        R<PlatformUserInfoEntity> platformedUser = remoteUserService.platformUser(name, SecurityConstants.FROM_IN);
        PlatformUserInfoEntity platformUserInfo = platformedUser.getData();
        if (platformUserInfo == null) {
            return null;
        }
        // 2. 查询当前人员列表
        return baseMapper.selectUserSessionList(platformUserInfo.getId());
    }

    @Override
    public Page<SessionInfoEntity> querySessionInfoPage(Page<SessionInfoEntity> page, Long userId) {
        R<PlatformUserInfoEntity> platformedUser = remoteUserService.platformUser(SecurityUtils.getUser().getName(), SecurityConstants.FROM_IN);
        PlatformUserInfoEntity platformUserInfo = platformedUser.getData();
        if (platformUserInfo == null) {
            return null;
        }
        LambdaQueryWrapper<SessionInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SessionInfoEntity::getSendUserId, platformUserInfo.getId())
                .eq(SessionInfoEntity::getReceiverUserId, userId)
                .or()
                .eq(SessionInfoEntity::getSendUserId, userId)
                .eq(SessionInfoEntity::getReceiverUserId, platformUserInfo.getId())
                .orderByDesc(SessionInfoEntity::getCreatedAt);
        Page<SessionInfoEntity> selectedPage = baseMapper.selectPage(page, wrapper);
        List<SessionInfoEntity> records = selectedPage.getRecords();
        Collections.reverse(records);
        selectedPage.setRecords(records);
        return selectedPage;
    }

    @Override
    public R chatOnLine(ChatVO chatVO, PlatformUserAccountEntity data) {
        // 1. 变更账户信息
        AccountChangeDTO accountChangeDTO = new AccountChangeDTO();
        //修改账户ID
        accountChangeDTO.setAccountId(chatVO.getAccountId());
        //变更数量
        accountChangeDTO.setChangeValue(chatVO.getECoinNum());
        //修改方法
        accountChangeDTO.setChangeMethod(ChangeMehtodEnum.SUBTRACT.name());
        //变更类型
        accountChangeDTO.setUsageType(UsageTypeEnum.E_COIN);
        R r = remoteAccountService.changeAccountInfo(accountChangeDTO, SecurityConstants.FROM_IN);
        if (r.getCode() != CommonConstants.SUCCESS) {
            return R.failed("变更账户数据失败");
        }
        // 2. 新增一条E币消费记录
        UsageRecordsEntity usageRecordsEntity = new UsageRecordsEntity();
        usageRecordsEntity.setUsageType(UsageTypeEnum.E_COIN);
        //发起调用者
        usageRecordsEntity.setUsageAccountId(chatVO.getAccountId());
        usageRecordsEntity.setAmount(chatVO.getECoinNum());
        //与哪个用户沟通
        usageRecordsEntity.setServiceId(chatVO.getChatWithUserId());
        usageRecordsEntity.setServiceName(chatVO.getChatWithUserName());
        //服务类型：在线聊天
        usageRecordsEntity.setServiceType(UsageServiceTypeEnum.CHAT_ONLINE);
        usageRecordsEntity.setUsageCompanyId(data.getCompanyId());
        R add = remoteUsageRecordsService.add(usageRecordsEntity, SecurityConstants.FROM_IN);
        if (add.getCode() != CommonConstants.SUCCESS) {
            return R.failed("新增消费记录失败");
        }
        return R.ok(Boolean.TRUE);
    }


}
